import type { Router } from 'vue-router'
import { accessRoutes } from './routes'
import { generateAccess } from './access'

// 通用路由守卫配置
function setupCommonGuard(router: Router) {
  // 记录已经加载的页面
  const loadedPaths = new Set<string>()

  router.beforeEach(async (to) => {
    to.meta.loaded = loadedPaths.has(to.path)

    // 页面加载进度条
    // if (!to.meta.loaded && preferences.transition.progress) {
    //   startProgress()
    // }
    return true
  })

  router.afterEach((to) => {
    // 记录页面是否加载,如果已经加载，后续的页面切换动画等效果不在重复执行
    loadedPaths.add(to.path)

    // 关闭页面加载进度条
    // if (preferences.transition.progress) {
    //   stopProgress()
    // }
  })
}

// 权限访问守卫配置
function setupAccessGuard(router: Router) {
  router.beforeEach(async (to: any, from: any, next: any) => {
    next()
  })
}
/**
 * 项目守卫配置
 * @param router
 */
function createRouterGuard(router: Router) {
  /** 通用 */
  setupCommonGuard(router)
  /** 权限访问 */
  setupAccessGuard(router)
}
export default createRouterGuard
